package cn.catsdad.service.impl;

import cn.catsdad.base.page.PageBean;
import cn.catsdad.base.page.PageParam;
import cn.catsdad.base.service.impl.BaseCrudServiceImpl;
import cn.catsdad.dto.PositionDTO;
import cn.catsdad.repository.PositionRepository;
import cn.catsdad.service.api.PositionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 岗位service实现
 *
 * @author xuyonglong
 * @version V1.0 2023/4/13
 */
@Service
public class PositionServiceImpl extends BaseCrudServiceImpl<PositionDTO, Long, PositionRepository>
        implements PositionService {

    @Override
    public void pageQuery(PageBean<PositionDTO> page, PageParam param) {
        page.setEntityName("PositionDTO p");
        page.setQuery("select p");
        super.pageQuery(page, param);
    }

    @Override
    @Transactional
    public PositionDTO save(PositionDTO entity) {
        validation(entity);
        return super.save(entity);
    }

    /**
     * 唯一性验证
     *
     * @param entity 保存的实体
     */
    private void validation(PositionDTO entity) {
        Integer count = repository.countByNameAndCodeAndIdNotAndIsDeletedFalse(entity.getName(), entity.getCode(), entity.getId());
        if (count > 0) {
            throw new RuntimeException("已存在当前岗位！");
        }
    }
}
